package com.lmt.org.service.impl;

import com.lmt.basic.utils.PageList;
import com.lmt.org.domain.Department;
import com.lmt.org.mapper.DepartmentMapper;
import com.lmt.org.query.DepartmentQuery;
import com.lmt.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @Description: TODO
* @Author: Amazing
* @Version: 1.0
* @Date:  2022/6/21 18:20
*/
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public List<Department> loadAll() {
        return departmentMapper.loadAll();
    }

    @Override
    public Department loadById(Long id) {
        return departmentMapper.loadById(id);
    }

    @Override
    @Transactional
    public void remove(Long id) {
        departmentMapper.remove(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(Department department) {
        // 在mapper.xml中添加方法开启主键返回
        departmentMapper.save(department);
        String dirPath = "";
        if (department.getParent() == null) {
            // 如果此部门的parent为null的话，就说明此部门是一个一级部门
            dirPath = ("/" + department.getId());
        }else {
            // 通过id查询到上级部门的信息
            Department parent = departmentMapper.loadById(department.getParent().getId());
            if (parent != null) {
                // 如果存在父级部门，那么把父级部门的dirPath和新添加的部门id拼接起来作为新部门的dirPath
                dirPath = parent.getDirPath() + "/" + department.getId();
            }
        }
        // 把拼接好的dirPath赋值到添加的对象
        department.setDirPath(dirPath);
        // 调用修改方法，更改dirPath
        departmentMapper.update(department);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Department department) {
        departmentMapper.update(department);
    }

    @Override
    public PageList<Department> queryData(DepartmentQuery departmentQuery) {
        List<Department> rows = departmentMapper.queryData(departmentQuery);
        Integer total = departmentMapper.queryCount(departmentQuery);
        return new PageList<>(total, rows);
    }

    @Override
    public Integer queryCount(DepartmentQuery departmentQuery) {
        return departmentMapper.queryCount(departmentQuery);
    }

    @Override
    public void batch(Long[] ids) {
        departmentMapper.batch(ids);
    }

    @Override
    public List<Department> tree() {
        // 获取所有部门信息
        List<Department> departments = departmentMapper.loadAll();
        // 将集合 数据转换为map集合
        Map<Long, Department> collect = departments.stream().collect(Collectors.toMap(Department::getId, item -> item));
        // 创建一个空集合，用来装处理后的部门树数据
        ArrayList<Department> depts = new ArrayList<>();
        // 遍历部门信息list集合
        for (Department department : departments) {
            // 判断：如果当前部门没有上级部门 ，那么此部门就是一级部门
            if (department.getParent_id() == null) {
                // 把一级部门装进集合中
                depts.add(department);
            }else {
                // 根据parent_id找到上级部门的信息
                Department parentDept = collect.get(department.getParent_id());
                if (parentDept != null) {
                    // 如果存在上一级，就把此子部门装到上一级部门中
                    parentDept.getChildren().add(department);
                }
            }
        }
        return depts;
    }
}
